C++ DOM读写xml(msxml6)

您所在的位置:网站首页 linux dom C++ DOM读写xml(msxml6)

C++ DOM读写xml(msxml6)

2024-07-16 01:34| 来源: 网络整理| 查看: 265

最近使用C++ DOM增删XML上的一个节点,以前没有写过,写的过程中也碰到一些问题,写完后整理了一下。运行在windows上,我用vs2015编译的。

参考了https://www.cnblogs.com/areliang/archive/2011/11/23/2260566.html

示例中,添加函数会创建如下的xml。

hei bei pei qi hua mao jia fei mao hei mao jing zhang #include #include #include #include #pragma comment(lib, "msxml6.lib") using namespace std; void CreateXmlAndAddNode() { CoInitialize(NULL); CComPtr spXmldoc; HRESULT hr = spXmldoc.CoCreateInstance(CLSID_DOMDocument60); if (SUCCEEDED(hr)) { CComPtr spDOMPI; hr = spXmldoc->createProcessingInstruction(L"xml", L"version='1.0' encoding='utf-8'", &spDOMPI); // 创建声明 if (hr == S_OK) { CComPtr outNode; spXmldoc->appendChild(spDOMPI, &outNode); //添加首行节点 outNode.Detach(); CComPtr pDOMRoot; hr = spXmldoc->createElement(L"Animals", &pDOMRoot); spXmldoc->appendChild(pDOMRoot, &outNode); //添加根节点 outNode.Detach(); if (hr == S_OK) { CComPtr textNode; spXmldoc->createTextNode(L"\n\t", &textNode); CComPtr pDOMElement1; pDOMRoot->appendChild(textNode, NULL); textNode.Detach(); spXmldoc->createElement(L"dog", &pDOMElement1); pDOMElement1->put_text(L"hei bei"); pDOMRoot->appendChild(pDOMElement1, NULL); spXmldoc->createTextNode(L"\n\t", &textNode); pDOMRoot->appendChild(textNode, NULL); textNode.Detach(); CComPtr pDOMElement2; spXmldoc->createElement(L"pig", &pDOMElement2); pDOMElement2->put_text(L"pei qi"); pDOMRoot->appendChild(pDOMElement2, NULL); spXmldoc->createTextNode(L"\n\t", &textNode); pDOMRoot->appendChild(textNode, NULL); textNode.Detach(); CComPtr pDOMElement3; spXmldoc->createElement(L"Cat", &pDOMElement3); pDOMElement3->put_text(L"hua mao"); pDOMRoot->appendChild(pDOMElement3, NULL); spXmldoc->createTextNode(L"\n\t", &textNode); pDOMRoot->appendChild(textNode, NULL); textNode.Detach(); CComPtr pDOMElement4; spXmldoc->createElement(L"Cat", &pDOMElement4); pDOMElement4->put_text(L"jia fei mao"); pDOMElement4->setAttribute(L"name", CComVariant("jiafei")); //添加属性 pDOMElement4->setAttribute(L"color", CComVariant("yellow")); pDOMRoot->appendChild(pDOMElement4, NULL); spXmldoc->createTextNode(L"\n\t", &textNode); pDOMRoot->appendChild(textNode, NULL); textNode.Detach(); CComPtr pDOMElement5; spXmldoc->createElement(L"Cat", &pDOMElement5); pDOMElement5->put_text(L"hei mao jing zhang"); pDOMElement5->setAttribute(L"name", CComVariant("jingzhang")); pDOMElement5->setAttribute(L"color", CComVariant("black")); pDOMRoot->appendChild(pDOMElement5, NULL); spXmldoc->createTextNode(L"\n", &textNode); pDOMRoot->appendChild(textNode, NULL); textNode.Detach(); } } spXmldoc->save(CComVariant("cppDOM.xml")); spXmldoc.Detach(); } CoUninitialize(); }

我的电脑里没有msxml4,我在system32下找到了msxml6.dll。试了一下也好用。需要引入这个库并包括头文件msxml6.h。这里要注意的是增加了一些空的textnode节点来换行,不加的话生成的xml内容只有一行。

删除函数://删除jia fei mao

这里第一次接触xpath,用它来定位节点很方便。

void DeleteNode() { CoInitialize(NULL); CComPtr spXmldoc; HRESULT hr = spXmldoc.CoCreateInstance(CLSID_DOMDocument60); if (SUCCEEDED(hr)) { VARIANT_BOOL isSuccessful; spXmldoc->load(CComVariant("cppDOM.xml"), &isSuccessful); if (VARIANT_TRUE == isSuccessful) { CComPtr spRoot = NULL; hr = spXmldoc->get_documentElement(&spRoot); if (hr == S_OK) { CComPtr spNode; //通过xpath寻找节点 hr = spRoot->selectSingleNode(L"/Animals/Cat[contains(@name, 'jiafei') and contains(@color, 'yellow')]", &spNode); if (hr == S_OK) { spRoot->removeChild(spNode, NULL);//删除节点 } } } } spXmldoc->save(CComVariant("cppDOM.xml")); spXmldoc.Detach(); CoUninitialize(); }

删除后的xml是。

hei bei pei qi hua mao hei mao jing zhang

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3